package com.lixh.virtual.ui.main.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseItemDraggableAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lixh.virtual.R;
import com.lixh.virtual.app.os.bean.AppData;
import com.lixh.virtual.app.os.bean.MultiplePackageAppData;
import com.lixh.virtual.utils.VUiKit;
import com.lixh.virtual.views.LabelView;
import com.lixh.virtual.views.LauncherIconView;

import java.util.ArrayList;

public class MainLaunchAdapter extends BaseItemDraggableAdapter<AppData, BaseViewHolder> {

    public MainLaunchAdapter() {
        super(R.layout.item_launcher_app, new ArrayList<>());
    }

    @Override
    protected void convert(BaseViewHolder holder, AppData data) {
        LauncherIconView iconView = holder.getView(R.id.item_app_icon_launcher);
        TextView nameView = holder.getView(R.id.item_app_name_launcher);
        LabelView spaceLabelView = holder.getView(R.id.item_app_space_idx);
        View firstOpenDot = holder.getView(R.id.item_first_open_dot);
        ImageView isHookIcon = holder.getView(R.id.item_app_ishook_launcher);

        iconView.setImageDrawable(data.getIcon());
        nameView.setText(data.getName());
        if (!data.isHook()) {
            if (data.isFirstOpen() && !data.isLoading()) {
                firstOpenDot.setVisibility(View.VISIBLE);
            } else {
                firstOpenDot.setVisibility(View.INVISIBLE);
            }
            isHookIcon.setVisibility(View.INVISIBLE);
        } else {
            isHookIcon.setVisibility(View.VISIBLE);
        }
        if (data instanceof MultiplePackageAppData) {
            MultiplePackageAppData multipleData = (MultiplePackageAppData) data;
            spaceLabelView.setVisibility(View.VISIBLE);
            spaceLabelView.setText(multipleData.userId + 1 + "");
        } else {
            spaceLabelView.setVisibility(View.INVISIBLE);
        }
        if (data.isLoading()) {
            startLoadingAnimation(iconView);
        } else {
            iconView.setProgress(100, false);
        }
    }

    private void startLoadingAnimation(LauncherIconView iconView) {
        iconView.setProgress(40, true);
        VUiKit.defer().when(() -> {
            try {
                Thread.sleep(900L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).done((res) -> iconView.setProgress(80, true));
    }
}